import java.lang.annotation.*;

// 自定义注解
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@interface TestAnnotation {}

@TestAnnotation
class Father {}

// 子类继承父类的所有注解
class Son extends Father {}

public class ANTest {
    public static void main(String[] args) {
        //获取单个注解
        System.out.println("获取单个注解=>" + Father.class.getAnnotation(TestAnnotation.class));
        System.out.println("获取单个注解=>" + Son.class.getAnnotation(TestAnnotation.class));

        //获取当前类的单个注解
        System.out.println("获取当前类的单个注解=>" + Father.class.getDeclaredAnnotation(TestAnnotation.class));
        System.out.println("获取当前类的单个注解=>" + Son.class.getDeclaredAnnotation(TestAnnotation.class));//null

        //获取本类和父类的注解
        Annotation[] annotations = Son.class.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("获取本类和父类的注解=>" + annotation.annotationType().getSimpleName());
        }

        //获取全部注解
        Annotation[] declaredAnnotations = Father.class.getDeclaredAnnotations();//Son.class获取为null
        for (Annotation annotation : declaredAnnotations) {
            System.out.println("获取全部注解=>" + annotation.annotationType().getSimpleName());
        }
    }
}

